package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.OrderDetailTiny;
import com.xmy.cultivate.entity.views.RecordCourseStudent;
import com.xmy.cultivate.entity.views.RecordCourseTiny;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.CourseMapper;
import com.xmy.cultivate.mapper.CourseSchedulingDetailMapper;
import com.xmy.cultivate.mapper.OrderDetailMapper;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.respons.ResSchoolDate;
import com.xmy.cultivate.service.ICourseSchedulingDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.ResultCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 排课详情 服务实现类
 * </p>
 *
 * @author min
 * @since 2022-08-01
 */
@Service
public class CourseSchedulingDetailServiceImpl extends ServiceImpl<CourseSchedulingDetailMapper, CourseSchedulingDetail> implements ICourseSchedulingDetailService {

    @Autowired
    CourseSchedulingDetailMapper courseSchedulingDetailMapper;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    CourseMapper courseMapper;

    public CourseSchedulingDetail getOneById(Long id){
        return courseSchedulingDetailMapper.getOneById(id);
    }

    /**
     *
     * @param wrapper
     * @param schedulingId 排课详情id
     * @return
     */
    public List<RecordCourseStudent> recordStudentList(Wrapper wrapper,Long schedulingId,Long gradeId,Long courseId,Long schoolId){

        List<RecordCourseStudent> recordCourseStudentList = courseSchedulingDetailMapper.recordStudentList(wrapper);
        for (RecordCourseStudent recordCourseStudent:recordCourseStudentList){
            //System.out.print("schedulingId:"+schedulingId);
            //System.out.print("recordCourseStudent.getId():"+recordCourseStudent.getId());

            //获取已记上课数据，由于不需要修改，这里暂时注释
            RecordCourseTiny recordCourseTiny = recordCourseMapper.getListForSchedulingIdAndStuId(schedulingId,recordCourseStudent.getId());
            if(recordCourseTiny == null){
                recordCourseStudent.setRecordCourseTiny(new RecordCourseTiny());
            }else {
                recordCourseStudent.setRecordCourseTiny(recordCourseTiny);
            }
            //recordCourseStudent.setRecordCourseTiny(new RecordCourseTiny());

            Course course = courseMapper.selectById(courseId);
            if(course == null){
                throw new CommonException(ResultCode.COURSE_NOT);
            }

            //剩余课时
            //OrderDetailTiny orderDetailTiny = orderDetailMapper.getResidueCourseCountForCourseId(recordCourseStudent.getId(),courseId,schoolId);
            OrderDetailTiny orderDetailTiny = orderDetailMapper.getResidueCourseCountForSubjectsId(recordCourseStudent.getId(),course.getSubjectsId(),schoolId);

            recordCourseStudent.setOrderDetailTiny(orderDetailTiny);
        }
        return recordCourseStudentList;
    }

    public List<CourseSchedulingDetail> findAll(Wrapper wrapper){
        List<CourseSchedulingDetail> courseSchedulingIPage = courseSchedulingDetailMapper.findAll(wrapper);
        return courseSchedulingIPage;
    }

    public IPage<CourseSchedulingDetail> findAllForPage(IPage<?> page,Wrapper wrapper){
        IPage<CourseSchedulingDetail> courseSchedulingIPage = courseSchedulingDetailMapper.findAllForPage(page,wrapper);
        return courseSchedulingIPage;
    }

    public List<ResSchoolDate> getSchoolDateList(String schoolId, String gradeId, String teacherId, String schoolDate,String endDate,String lessonType){
        return courseSchedulingDetailMapper.getSchoolDateList(schoolId,gradeId,teacherId,schoolDate,endDate,lessonType);
    }

    public CourseSchedulingDetail getCourseSchedulingDetailById(Long id){
        return courseSchedulingDetailMapper.getCourseSchedulingDetailById(id);
    }

    @Override
    public List<CourseSchedulingDetail> getCourseSchedulingDetailList(Wrapper wrapper){
        return courseSchedulingDetailMapper.getCourseSchedulingDetailList(wrapper);
    }
}
